package com.lagou.edu.service.impl;

import com.lagou.edu.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


/**
 * @author 陶肖寅
 * @version V1.0
 * <p>
 *     验证码业务实现层
 * @date 2020-05-06 20:17
 * @email fromlx@126.com
 * @Copyright © 2020-2021 城云科技(中国)有限公司
 * </p>
 */
@Service
public class CodeService implements ICodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String generateCode() {
        int code = (int)((Math.random()*9+1)*100000);
        return String.valueOf(code);
    }

    @Override
    public void saveCode(String email, String code) {
        redisTemplate.opsForValue().set(email,code,10, TimeUnit.MINUTES);

    }


    @Override
    public String getCode(String email) {
        String code = (String) redisTemplate.opsForValue().get(email);
        if (code ==null || "".equals(code)) {
            return null;
        }
        return code;
    }


}
